package week2;

public class Polygon {
	
	double [] xpoints, ypoints;
	int npoints;
	
	//this is the Polygon constructor
	//the if statement allows the constructor to abort if the array does not make a polygon.
	public Polygon(double [] xpoints, double [] ypoints, int npoints){
		
		if (xpoints[0]==xpoints[npoints-1] 
				&& ypoints[0]==ypoints[npoints-1]){
		
		
			this.xpoints = xpoints;
			this.ypoints = ypoints;
			this.npoints = npoints;
			System.out.println("This is a polygon, the first and last coordinates match.");
		}
		else
			this.xpoints = xpoints;
			this.ypoints = ypoints;
			this.npoints = npoints;
			System.out.println("This is not a polygon, the first and last coordinates do not match.");
			
	}
	
	//same as the PolyLine addPoint code except for it makes sure the new point closes the Polygon.
	//this assumes that the user is always adding the "last" point to a polygon.
	public void addPoint(double userX, double userY){
		double [] xtemp = new double[npoints+1];
		double [] ytemp = new double[npoints+1];
		
		for (int i=0; i<npoints; i++){
		xtemp[i] = xpoints[i];
		ytemp[i] = ypoints[i];
		}			
		xtemp[npoints] = userX;
		ytemp[npoints] = userY;
		
		xpoints=xtemp;
		ypoints=ytemp;
		npoints=npoints+1;
		
		if (xpoints[0]==xpoints[npoints-1] 
				&& ypoints[0]==ypoints[npoints-1]){
		
		System.out.println("This is a polygon, the first and last coordinates match.");
		}
		else
			System.out.println("This is not a polygon, the first and last coordinates do not match.");
	}
	
}

